<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <xsl:output method="html" />
  <!-- global variables -->
  <xsl:variable name="action_noChange" select="'no change'" />
  <xsl:variable name="action_increase" select="'increase'" />
  <xsl:variable name="unit_percentage" select="'percentage'" />
  <xsl:variable name="adjust_doc_filename"
  select="'priceAdjuster.xml'" />
  <!-- root template -->
  <xsl:template match="/">
    <html>
      <head>
        <title>Juicers Part 01-A</title>
      </head>
      <body>
        <xsl:variable name="adjustDoc"
        select="document($adjust_doc_filename)" />
        <h2>
          <font color="darkblue">Result for 
          <xsl:value-of select="$adjust_doc_filename" /></font>
        </h2>
        <br clear="left" />
        <table border="1" align="left">
          <thead>
            <tr>
              <th bgcolor="black">
                <font color="white">Juicer</font>
              </th>
              <th bgcolor="black">
                <font color="white">Cost</font>
              </th>
            </tr>
          </thead>
          <tbody>
            <xsl:for-each select="juicers/juicer">
              <tr>
                <td>
                  <xsl:value-of select="name" />
                </td>
                <td>
                  <!-- Output cost, checking for updates to price from external file -->
                  <xsl:variable name="adjustAction"
                  select="$adjustDoc/priceAdjustment/adjustment/@action" />
                  <xsl:choose>
                    <xsl:when test="$adjustAction = $action_noChange">
                      <xsl:value-of select="format-number(cost[@currency='USD'], '$##,###0.00')" />
                    </xsl:when>
                    <xsl:when test="$adjustAction = $action_increase">
                      <xsl:variable name="unit"
                      select="$adjustDoc/priceAdjustment/adjustment/sizeIncrease/@units" />
                      <xsl:choose>
                        <xsl:when test="$unit = $unit_percentage">
                          <xsl:variable name="base"
                          select="cost[@currency='USD']" />
                          <xsl:variable name="amt"
                          select="$adjustDoc/priceAdjustment/adjustment/sizeIncrease" />
                          <xsl:variable name="jCost"
                          select="$base + ($base * ($amt div 100))" />
                          <xsl:value-of select="format-number($jCost, '$##,###0.00')" />
                        </xsl:when>
                        <!-- other unit types go here -->
                        <xsl:otherwise>
                          <xsl:message terminate="yes">
                            <xsl:text>Invalid value &quot;</xsl:text>
                            <xsl:value-of select="$unit" />
                            <xsl:text>&quot; specified for attribute 'units'.</xsl:text>
                          </xsl:message>
                        </xsl:otherwise>
                      </xsl:choose>
                    </xsl:when>
                    <!-- other action types go here -->
                    <xsl:otherwise>
                      <xsl:message terminate="yes">
                        <xsl:text>Invalid value &quot;</xsl:text>
                        <xsl:value-of select="$adjustAction" />
                        <xsl:text>&quot; specified for attribute 'action'.</xsl:text>
                      </xsl:message>
                    </xsl:otherwise>
                  </xsl:choose>
                </td>
              </tr>
            </xsl:for-each>
          </tbody>
        </table>
        <br clear="left" />
        <br clear="left" />
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
